/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.repository;

import me.zhengjie.modules.anniversary.domain.Department;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-22
**/
public interface DepartmentRepository extends JpaRepository<Department, Long>, JpaSpecificationExecutor<Department> {

    /**
     * 获取院系架构的可选年份
     * @return /
     */
    @Query(value = "SELECT DISTINCT year FROM gzmu_ann_department ORDER BY year",nativeQuery = true)
    List<Integer> getDepartmentYears();

    /**
     * 根据 ParentId 查询
     * @param id parent_id
     * @return /
     */
    List<Department> findByParentId(Long id);

    /**
     * 获取顶级部门
     * @return /
     */
    List<Department> findByParentIdIsNull();

    /**
     * 判断是否存在子节点
     * @param parent_id /
     * @return /
     */
    int countByParentId(Long parent_id);

    /**
     * 根据ID更新sub_count
     * @param count /
     * @param id /
     */
    @Modifying
    @Query(value = " update gzmu_ann_department set sub_count = ?1 where id = ?2 ",nativeQuery = true)
    void updateSubCntById(Integer count, Long id);

    @Query(value = "SELECT * FROM gzmu_ann_department WHERE year=?1 AND type='2'",nativeQuery = true)
    List<Department> findMajorsByYear(Integer year);
}